<?php
/**
 * @version		$Id: mydb.php 2011.08.11 thierry $
 * @package		site.model
 * @subpackage	com_projid
 * @copyright	Copyright (C) 2011 Pojid All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */
defined('_JEXEC') or die('Restricted access');
 class Mydb{

	private $_result;
	private $_item;
	private $_requete;

public function __construct($requete){
	$this->_requete = $requete;
	$this->setQuery();
	$this->setResult();
	}

function getItem(){
	return $this->_item;
	}

function getResult(){
	return $this->_result;
	}

function setQuery() {
		$requete = $this->_requete;

		$db = JFactory::getDBO();

		$query = $db->getQuery(true);

		$query->select('*');
		$query->from('#__projid_server');
		$query->leftJoin('#__projid_requete on #__projid_server.id_serv=#__projid_requete.server_id') ;
		$query->where('#__projid_requete.id_que='.$requete.';');
		$db->setQuery((string)$query);
			$entries = $db->loadObject();
			$this->_item=$entries;
	return $this->_item;

	
		}

function setResult(){
		$this->_item;
		//
		$item = $this->_item;
		$option = array(
		'host' 		=> $item->host,
		'database'	=> $item->base,
		'user' 		=> $item->user,
		'password' 	=> $item->password,
		'driver' 	=> $item->driver
		//'prefix'	=> $item->prefix
		);
		$query = $item->requete;

		
	if ($item->driver=="postgres")
	{
		$postgres=new mypostgresql($item);
		$postgres->connection();
		$postgres->DoRequete();
		$result=$postgres->_result;
	}
	else
	{
		$db = JDatabase::getInstance($option);
		$jAp=& JFactory::getApplication();

		if (JError::isError($db)) {
					header('HTTP/1.1 500 Internal Server Error');
					jexit('Database Error: ' . (string) $db);
				}
		if ($db->getErrorNum() > 0) {
			JError::raiseError(500, JText::sprintf('JLIB_UTIL_ERROR_CONNECT_DATABASE', $db->getErrorNum(), $db->getErrorMsg()));
			//echo $db->getErrorMsg();
				}
				
		$db->setQuery($query); // Récupère la requête a exécuter avec l'info du serveur

		$db->query();
		
		$result = $db->loadRowList();
		
		if (is_null($posts=$db->loadRowList())) {
			$jAp->enqueueMessage(nl2br($db->getErrorMsg()),'error'); 
			return;
		}
	}
		return $this->_result=$result;
	}
}